package com.wc.fortnight_blue_bridge.Q241005.E_倒斗;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.StringTokenizer;

/**
 * @Author congge
 * @Date 2024/10/5 22:39
 * @description https://www.lanqiao.cn/problems/19864/learning/?contest_id=210
 */
public class Main {
    /**
     * 思路：<p>
     * 线段树, 查找i, j<p>
     * 遍历每一个 i 后面插板, 如果a[i]第一次出现,那么后面 第二次出现的a[i] 到 最后一次出现的 a[i]之间插板是都可以使得满意度 +1<p>
     * 如果不是第一次出现, 那么 a[i] 出现,到达下一次出现的a[i]的之间的板子满意度 -1, 因为这样插在这后面是不存在满意度的
     */
    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static int N = 200010;
    static int[] maxv = new int[N << 2], lazy = new int[N << 2];
    static int[] a = new int[N];
    static int[] cnt = new int[N];
    static ArrayList<Integer>[] idx = new ArrayList[N];
    static int n;

    public static void main(String[] args) {
        n = sc.nextInt();
        for (int i = 0; i < N; i++) idx[i] = new ArrayList<>();
        for (int i = 1; i <= n; i++) {
            a[i] = sc.nextInt();
            idx[a[i]].add(i);
        }

        int res = 0;
        for (int i = 1; i < n; i++) {
            cnt[a[i]]++;
            int size = idx[a[i]].size();
            if (cnt[a[i]] == 1) {
                // 大于3才有机会增加满意度, 假设每个板子就在 i 后面
                if (size >= 3) {
                    int l = idx[a[i]].get(cnt[a[i]]), r = idx[a[i]].get(size - 1);
                    add(1, 1, n, l, r - 1, 1);
                    res = Math.max(res, query(1, 1, n, l, r - 1));
                }
            } else {
                if (cnt[a[i]] < size) {
                    int l = idx[a[i]].get(cnt[a[i]] - 1), r = idx[a[i]].get(cnt[a[i]]);
                    add(1, 1, n, l, r - 1, -1);
                }
            }
        }
        out.println(res);
        out.flush();
    }

    static int query(int u, int l, int r, int ql, int qr) {
        if (l == ql && r == qr) {
            return maxv[u];
        }
        pushDown(u);
        int mid = l + r >> 1;
        if (qr <= mid) return query(u << 1, l, mid, ql, qr);
        else if (ql > mid) return query(u << 1 | 1, mid + 1, r, ql, qr);

        int left = query(u << 1, l, mid, ql, mid);
        int right = query(u << 1 | 1, mid + 1, r, mid + 1, qr);
        return Math.max(left, right);
    }

    static void add(int u, int l, int r, int ql, int qr, int c) {
        if (l == ql && r == qr) {
            maxv[u] += c;
            lazy[u] += c;
            return;
        }
        pushDown(u);
        int mid = l + r >> 1;
        if (qr <= mid) add(u << 1, l, mid, ql, qr, c);
        else if (ql > mid) add(u << 1 | 1, mid + 1, r, ql, qr, c);
        else {
            add(u << 1, l, mid, ql, mid, c);
            add(u << 1 | 1, mid + 1, r, mid + 1, qr, c);
        }
        pushUp(u);
    }

    static void pushUp(int u) {
        maxv[u] = Math.max(maxv[u << 1], maxv[u << 1 | 1]);
    }

    static void pushDown(int u) {
        if (lazy[u] != 0) {
            maxv[u << 1] += lazy[u];
            maxv[u << 1 | 1] += lazy[u];
            lazy[u << 1] += lazy[u];
            lazy[u << 1 | 1] += lazy[u];
            lazy[u] = 0;
        }
    }
}

class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    String nextLine() {
        String s = "";
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    // 是否由下一个
    boolean hasNext() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                String line = br.readLine();
                if (line == null)
                    return false;
                st = new StringTokenizer(line);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return true;
    }
}
